package tt.dz.appserver.service.business.imp;

import java.util.List;

import javax.annotation.Resource;

import org.apache.avro.AvroRemoteException;

import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.dao.ext.LabelDao;
import tt.dz.appserver.entity.model.pub.Label;
import tt.dz.appserver.service.business.LabelService;
import tt.dz.appserver.util.encoding.EncodingUtils;
import tt.dz.appserver.util.enums.Response;

public class LabelServiceImpl implements LabelService {

	@Resource
	private LabelDao labelDao;
	
	@Override
	public JSONObject publabeladd(Integer labeltype, String labelname) throws AvroRemoteException {
		// TODO Auto-generated method stub
		Label label = new Label();
		JSONObject resultJsonObject = new JSONObject();
		
		String latestLabelId = labelDao.findLatestLabelId();
		String labelid;
		
		if (latestLabelId == null) {
			labelid = "00001";
		} else {
			labelid = EncodingUtils.nextSequenceString(latestLabelId);
		}
		
		label.setLabel_id(labelid);
		label.setLabel_type(labeltype);
		label.setLabel_name(labelname);
		
		labelDao.addLabel(label);
		
		resultJsonObject.put("labelid", labelid);
		resultJsonObject.put("labeltype", labeltype);
		resultJsonObject.put("labelname", labelname);
		resultJsonObject.put("code", Response.SUCCESS.getCode());
		resultJsonObject.put("msg", Response.SUCCESS.getMsg());
		
		return resultJsonObject;
	}

	@Override
	public JSONObject publabeldel(String labelid) throws AvroRemoteException {
		// TODO Auto-generated method stub
		Label label = new Label();
		JSONObject resultJsonObject = new JSONObject();
		
		label.setLabel_id(labelid);
		
		labelDao.delLabel(label);
		
		resultJsonObject.put("labelid", labelid);
		resultJsonObject.put("code", Response.SUCCESS.getCode());
		resultJsonObject.put("msg", Response.SUCCESS.getMsg());
		
		return resultJsonObject;
	}

	@Override
	public JSONObject publabelupdate(String labelid, Integer labeltype, String labelname) throws AvroRemoteException {
		// TODO Auto-generated method stub
		Label label = new Label();
		JSONObject resultJsonObject = new JSONObject();
		
		label.setLabel_id(labelid);
		label.setLabel_type(labeltype);
		label.setLabel_name(labelname);
		
		labelDao.updateLabel(label);
		
		resultJsonObject.put("labelid", labelid);
		resultJsonObject.put("labeltype", labeltype);
		resultJsonObject.put("labelname", labelname);
		resultJsonObject.put("code", Response.SUCCESS.getCode());
		resultJsonObject.put("msg", Response.SUCCESS.getMsg());
		
		return resultJsonObject;
	}

	@Override
	public List<Label> publabelselect() throws AvroRemoteException {
		// TODO Auto-generated method stub
		
		return labelDao.findLabel();
	}

}
